/*
 *
 * Copyright (c) 2023 - future
 * @author fzh
 * @email e_341877120_mail@163.com
 * @link https://mvnrepository.com/search?q=io.github.xrfzh.cn
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.xrfzh.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.xrfzh.common.annotation.SystemLog;
import cn.xrfzh.common.enums.LogType;
import com.xr.common.utils.R;
import com.xr.qiniuyun.service.QiniuyunFileService;
import cn.xrfzh.system.common.base.MyPage;
import cn.xrfzh.system.common.base.Result;
import cn.xrfzh.system.common.groups.Save;
import cn.xrfzh.system.common.groups.Update;
import cn.xrfzh.system.common.utils.CurrentLoginUserUtil;
import cn.xrfzh.system.entity.user.AdminMenuVo;
import cn.xrfzh.system.entity.user.AssociatedRoleDto;
import cn.xrfzh.system.entity.user.PasswordDto;
import cn.xrfzh.system.entity.user.User;
import cn.xrfzh.system.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 总后台用户表(User)表控制层
 */
@Validated
@RestController
@RequestMapping("/api/v1/account")
public class UserController {

	/**
	 * 服务对象
	 */
	@Resource
	private UserService userService;

	@Resource
	private QiniuyunFileService qiniuyunFileService;

	/**
	 * 上传用户头像
	 * @param file
	 * @return
	 */
	@SystemLog(module = "用户模块", type = LogType.UPLOAD, description = "上传用户头像")
	@PostMapping("/uploadAvatar")
	public R uploadFile(@RequestBody MultipartFile file) {
		return qiniuyunFileService.uploadFile(file);
	}

	/**
	 * 更新当前登录用户密码
	 * @param passwordDTO
	 * @return
	 */
	@SystemLog(module = "用户模块", type = LogType.UPDATE, description = "更新当前登录用户密码")
	public Result<Object> setPassword(@RequestBody PasswordDto passwordDTO) {
		return Result.isSuccess(userService.setPassword(passwordDTO));
	}

	/**
	 * 给用户分配系统角色
	 * @param associatedRoleDTO
	 * @return
	 */
	@SystemLog(module = "用户模块", type = LogType.INSERT, description = "给用户分配系统角色")
	@PostMapping("/associatedRole")
	public Result<Object> associatedRole(@RequestBody AssociatedRoleDto associatedRoleDTO) {
		return Result.isSuccess(userService.associatedRole(associatedRoleDTO));
	}

	/**
	 * 启用/禁用用户账号
	 * @param accountId
	 * @return
	 */
	@SystemLog(module = "用户模块", type = LogType.UPDATE, description = "启用/禁用用户账号")
	@PutMapping("/disOrEnaAccount")
	public Result<Object> disOrEnaAccount(@NotNull(message = "账号id不能为空") Long accountId) {
		return Result.isSuccess(userService.disOrEnaAccount(accountId));
	}

	/**
	 * 获取当前登录人菜单
	 * @return
	 */
	@GetMapping("/menu")
	public Result<List<AdminMenuVo>> getMenuList() {
		return Result.success(userService.getMenuList());
	}

	/**
	 * 查询当前登录人信息
	 * @return
	 */
	@GetMapping("/userInfo")
	public Result<User> userInfo() {
		return Result.success(userService.getById(CurrentLoginUserUtil.getUserId()));
	}

	/**
	 * 查询所有数据
	 * @param user 查询实体
	 * @return 所有数据
	 */
	@GetMapping("/queryAll")
	public Result<List<User>> queryAll(User user) {
		user.setEnable(true);
		return Result.success(userService.list(new QueryWrapper<>(user)));
	}

	/**
	 * 分页查询所有数据
	 * @param page 分页对象
	 * @param user 查询实体
	 * @return 所有数据
	 */
	@GetMapping("/queryByPage")
	public Result<MyPage<User>> queryByPage(MyPage<User> page, User user) {
		return Result.success(userService.page(page, new QueryWrapper<>(user)));
	}

	/**
	 * 通过主键查询单条数据
	 * @param id 主键
	 * @return 单条数据
	 */
	@GetMapping("/queryOne/{id}")
	public Result<User> selectOne(@PathVariable Serializable id) {
		return Result.success(userService.getById(id));
	}

	/**
	 * 新增用户
	 * @param user 实体对象
	 * @return 新增结果
	 */
	@SystemLog(module = "用户模块", type = LogType.INSERT, description = "新增用户")
	@PostMapping("/insert")
	public Result<Object> insert(@Validated(Save.class) @RequestBody User user) {
		return Result.isSuccess(userService.insert(user));
	}

	/**
	 * 更新用户
	 * @param user 实体对象
	 * @return 修改结果
	 */
	@SystemLog(module = "用户模块", type = LogType.UPDATE, description = "更新用户")
	@PutMapping("/update")
	public Result<Object> update(@Validated(Update.class) @RequestBody User user) {
		return Result.isSuccess(userService.update(user));
	}

	/**
	 * 删除用户
	 * @param id 主键
	 * @return 删除结果
	 */
	@SystemLog(module = "用户模块", type = LogType.DELETE, description = "删除用户")
	@DeleteMapping("/delete/{id}")
	public Result<Object> delete(@PathVariable Long id) {
		User user = new User();
		LambdaUpdateWrapper<User> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(User::getId, id).eq(User::getInitial, false);
		return Result.isSuccess(userService.deleteWithFill(user, updateWrapper));
	}

	/**
	 * 批量删除用户
	 * @param idList 主键结合
	 * @return 删除结果
	 */
	@SystemLog(module = "用户模块", type = LogType.DELETE, description = "批量删除用户")
	@DeleteMapping("/batchDelete")
	public Result<Object> batchDelete(
			@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
		User user = new User();
		user.setInitial(false);
		return Result.isSuccess(userService.batchDelete(idList, user));
	}

	/**
	 * 批量设置系统内置用户
	 * @param idList
	 * @return
	 */
	@SystemLog(module = "用户模块", type = LogType.UPDATE, description = "批量设置系统内置用户")
	@PutMapping("/setInitAccount")
	public Result<Object> setInitAccount(@RequestBody List<Long> idList) {
		return Result.isSuccess(userService.setInitAccount(idList));
	}

	/**
	 * 获取全部内置账户id
	 * @return
	 */
	@GetMapping("/listInitAccount")
	public Result<List<Object>> listInitAccount() {
		LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(User::getInitial, true).eq(User::getEnable, true);
		queryWrapper.select(User::getId);
		return Result.success(userService.listObjs(queryWrapper));
	}

	/**
	 * 获取验证码
	 * @param response
	 */
	@GetMapping("/verificationCode")
	public void verification(HttpServletResponse response) {
		userService.getVerification(response);
	}

}
